#include <iostream>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> map;

        int i = 0;
        int j = 0;
        int max = 0;

        for (auto it = s.begin(); it != s.end(); ++it) {
            char ch = *it;
            auto it2 = map.find(ch);

            if (it2 == map.end()) {
                map.insert(std::make_pair(ch, i));
            } else if (it2->second < j) {
                it2->second = i;
            } else {
                j = it2->second + 1;
                it2->second = i;
            }

            int cur = i - j + 1;
            if (cur > max) {
                max = cur;
            }

            i++;
        }
        return max;
    }
};

int main(int argc, char *argv[]) {
    Solution sl;
    cout << sl.lengthOfLongestSubstring("abcabcbb") << endl;
    return 0;
}